package com.ruoyi.drone.controller;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.drone.utils.PhotoUploadUtils;

/**
 * 无人机系统照片上传Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/drone/photo")
public class DronePhotoController extends BaseController
{
    @Value("${ruoyi.profile}")
    private String uploadPath;

    /**
     * 上传身份证正面照片
     */
    @PostMapping("/upload/idcard/front")
    public AjaxResult uploadIdCardFront(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "idcard_front", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传身份证反面照片
     */
    @PostMapping("/upload/idcard/back")
    public AjaxResult uploadIdCardBack(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "idcard_back", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传探险信息照片
     */
    @PostMapping("/upload/exploration")
    public AjaxResult uploadExploration(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "exploration", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传个人资质照片
     */
    @PostMapping("/upload/qualification")
    public AjaxResult uploadQualification(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "qualification", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传证书照片
     */
    @PostMapping("/upload/certificate")
    public AjaxResult uploadCertificate(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "certificate", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传技能证明照片
     */
    @PostMapping("/upload/skill")
    public AjaxResult uploadSkill(@RequestParam("file") MultipartFile file)
    {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "skill_proof", uploadPath);
            return success(photoUrl);
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 删除照片
     */
    @PostMapping("/delete")
    public AjaxResult deletePhoto(@RequestParam("photoUrl") String photoUrl)
    {
        try {
            boolean success = PhotoUploadUtils.deletePhoto(photoUrl, uploadPath);
            if (success) {
                return success("删除成功");
            } else {
                return error("删除失败");
            }
        } catch (Exception e) {
            return error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 验证照片URL
     */
    @PostMapping("/validate")
    public AjaxResult validatePhoto(@RequestParam("photoUrl") String photoUrl)
    {
        try {
            boolean valid = PhotoUploadUtils.isValidPhotoUrl(photoUrl);
            return success(valid);
        } catch (Exception e) {
            return error("验证失败：" + e.getMessage());
        }
    }
}
